/*
 * StartGui.java
 *
 * Created on 06 April 2003, 07:04
 * Copyright (C) 2003, Velocityme Partnership
 */

package com.velocityme.client;

import javax.ejb.*; 
import java.util.*;
import com.velocityme.client.gui.*;
import com.velocityme.client.gui.GuiLogin;
import com.velocityme.client.gui.access.GuiDisplayCreateGroupAccess;
import com.velocityme.client.gui.access.GuiDisplayGroupAccessByNode;
import com.velocityme.client.gui.contactdetail.ContactDetailTypeControl;
import com.velocityme.client.gui.node.contactable.department.DepartmentControl;
import com.velocityme.client.gui.node.contactable.organisation.OrganisationControl;
import com.velocityme.client.gui.node.contactable.person.PersonControl;
import com.velocityme.client.gui.node.NodeControl;
import com.velocityme.client.gui.node.role.RoleControl;
import com.velocityme.client.gui.node.state.StateControl;
import com.velocityme.client.gui.node.status.StatusControl;
import com.velocityme.client.gui.node.task.action.ActionControl;
import com.velocityme.client.gui.node.task.issue.IssueControl;
import com.velocityme.client.gui.node.task.project.ProjectControl;
import com.velocityme.client.gui.node.taskstatemachine.TaskStateMachineControl;
import com.velocityme.client.gui.node.user.UserControl;
import com.velocityme.client.gui.time.TimeControl;
import com.velocityme.client.gui.time.TimeReportPanel;
import com.velocityme.entity.PermissionBean;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject; 

import com.velocityme.interfaces.*;
import com.velocityme.client.utility.*;
import com.velocityme.valueobjects.ContactableValue;
import com.velocityme.valueobjects.NodeValue;
import com.velocityme.valueobjects.TimeValue;
import java.awt.Component;
import java.lang.reflect.UndeclaredThrowableException;
import java.rmi.RemoteException;
import javax.naming.NamingException;
import javax.swing.UIManager;
import javax.swing.tree.*;
import com.velocityme.client.gui.exception.*;
import com.velocityme.client.gui.node.group.GroupControl;
import com.velocityme.client.gui.node.task.anchor.AnchorControl;
import com.velocityme.client.gui.search.GuiDisplayCompletedTasks;
import com.velocityme.client.gui.search.GuiDisplayRecentTasks;
import com.velocityme.utility.InvalidKeyException;
import com.velocityme.utility.PermissionDeniedException;
import javax.swing.JOptionPane;

/**
 *
 * @author  Robert Crida Work
 */
public class StartGui extends javax.swing.JFrame {
    
    KeySession m_key;
    
    NodeValue m_nodeValue;
 
    //popup and cut and paste object
    DefaultMutableTreeNode m_cutNode = null;
    com.velocityme.client.gui.menu.popup.NavigationTreePopup m_popup = new com.velocityme.client.gui.menu.popup.NavigationTreePopup();
   
    /** Creates new form StartGui */
    public StartGui() {
        initComponents();
        setupPopupListeners();
        // begin by putting up a login dialog and waiting for it to complete
        GuiLogin login = new GuiLogin(this, true);
        login.setLocationRelativeTo(null);
        login.show();
        // now get the key that was created during the login procedure
        m_key = login.getKey();
        try {
            // check if a valid login key has been created
            if (m_key.isValid()) {
                try {
                    DefaultTreeCellRenderer rend1 = new DefaultTreeCellRenderer();
                    GuiTreeCellRenderer rend2 = new GuiTreeCellRenderer(rend1.getOpenIcon(), rend1.getClosedIcon(), rend1.getLeafIcon());
                    jTreeNode.setCellRenderer(rend2);
                    // display current person and user name
                    jLabelPersonValue.setText(m_key.getPersonName());
                    jLabelUserValue.setText(m_key.getUserName());
                    // get all the nodes that are visible at the root of the tree
                    Collection rootChildren = StatelessRemoteClientSessionUtil.getSession().getVisibleChildrenValues(m_key, null);
                    Iterator it = rootChildren.iterator();
                    while(it.hasNext()) {
                        m_guiRootNode.add(new DefaultMutableTreeNode(new GuiTreeNode(m_key, (NodeValue)it.next())));
                    }
                    m_guiModelTree.nodeStructureChanged(m_guiRootNode);
                    jTabbedPane.removeAll();
                    jTabbedPane.addTab("Browse", null);
                }
                catch(Exception e){
                    ExceptionWindow.showError(e);
                }
            }
        }
        catch(RemoteException e) {
            ExceptionWindow.showError(e);
        }
    }
    
    private void addTab(String p_title, Component p_component) {
        jTabbedPane.addTab(p_title, p_component);
        jTabbedPane.setSelectedComponent(p_component);        
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        jFrame1 = new javax.swing.JFrame();
        jSplitPane1 = new javax.swing.JSplitPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        m_guiRootNode = new DefaultMutableTreeNode(new Object());
        m_guiModelTree = new DefaultTreeModel(m_guiRootNode);
        jTreeNode = new javax.swing.JTree(m_guiModelTree);
        jTreeNode.putClientProperty("JTree.lineStyle", "Angled");
        //jTreeNode.setCellRenderer(new NodeTreeCellRenderer());
        jPanel1 = new javax.swing.JPanel();
        jTabbedPane = new javax.swing.JTabbedPane();
        jToolBar1 = new javax.swing.JToolBar();
        jButtonNode = new javax.swing.JButton();
        jButtonProject = new javax.swing.JButton();
        jButtonAnchor = new javax.swing.JButton();
        jButtonAction = new javax.swing.JButton();
        jButtonIssue = new javax.swing.JButton();
        jButtonOrganisation = new javax.swing.JButton();
        jButtonDepartment = new javax.swing.JButton();
        jButtonPerson = new javax.swing.JButton();
        jButtonUser = new javax.swing.JButton();
        jButtonGroup = new javax.swing.JButton();
        jButtonTaskType = new javax.swing.JButton();
        jButtonState = new javax.swing.JButton();
        jButtonStatus = new javax.swing.JButton();
        jButtonRole = new javax.swing.JButton();
        jButtonUserAccess = new javax.swing.JButton();
        jButtonGroupAccess = new javax.swing.JButton();
        jButtonNodeUserAccess = new javax.swing.JButton();
        jButtonNodeGroupAccess = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabelPerson = new javax.swing.JLabel();
        jLabelPersonValue = new javax.swing.JLabel();
        jLabelUser = new javax.swing.JLabel();
        jLabelUserValue = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenuFile = new javax.swing.JMenu();
        jMenuItemLogout = new javax.swing.JMenuItem();
        jMenuItemExit = new javax.swing.JMenuItem();
        jMenuSearch = new javax.swing.JMenu();
        jMenuItemMyResponsibleTasks = new javax.swing.JMenuItem();
        jMenuItemMyInterestedTasks = new javax.swing.JMenuItem();
        jMenuItemNewTasks = new javax.swing.JMenuItem();
        jMenuItemActiveTasks = new javax.swing.JMenuItem();
        jMenuItemCompletedTasks = new javax.swing.JMenuItem();
        jMenuItemParameters = new javax.swing.JMenuItem();
        jMenuItemTime = new javax.swing.JMenuItem();
        jMenuHelp = new javax.swing.JMenu();
        jMenuItemAbout = new javax.swing.JMenuItem();

        setTitle("Velocityme Client");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });

        jScrollPane1.setPreferredSize(new java.awt.Dimension(200, 400));
        jTreeNode.setRootVisible(false);
        jTreeNode.setShowsRootHandles(true);
        jTreeNode.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTreeNodeMouseClicked(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jTreeNodeMouseReleased(evt);
            }
        });

        jScrollPane1.setViewportView(jTreeNode);

        jSplitPane1.setLeftComponent(jScrollPane1);

        jPanel1.setLayout(new java.awt.BorderLayout());

        jPanel1.setPreferredSize(new java.awt.Dimension(400, 400));
        jPanel1.add(jTabbedPane, java.awt.BorderLayout.CENTER);

        jSplitPane1.setRightComponent(jPanel1);

        getContentPane().add(jSplitPane1, java.awt.BorderLayout.CENTER);

        jButtonNode.setFont(new java.awt.Font("Dialog", 1, 10));
        jButtonNode.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/velocityme/client/icons/node-new-32x32.png")));
        jButtonNode.setToolTipText("Create new node");
        jButtonNode.setEnabled(false);
        jButtonNode.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonNodeActionPerformed(evt);
            }
        });

        jToolBar1.add(jButtonNode);

        jButtonProject.setFont(new java.awt.Font("Dialog", 1, 10));
        jButtonProject.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/velocityme/client/icons/project-new-32x32.png")));
        jButtonProject.setToolTipText("Create new project");
        jButtonProject.setEnabled(false);
        jButtonProject.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonProjectActionPerformed(evt);
            }
        });

        jToolBar1.add(jButtonProject);

        jButtonAnchor.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/velocityme/client/icons/anchor-new-32x32.png")));
        jButtonAnchor.setEnabled(false);
        jButtonAnchor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAnchorActionPerformed(evt);
            }
        });

        jToolBar1.add(jButtonAnchor);

        jButtonAction.setFont(new java.awt.Font("Dialog", 1, 10));
        jButtonAction.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/velocityme/client/icons/action-new-32x32.png")));
        jButtonAction.setToolTipText("Create new action");
        jButtonAction.setEnabled(false);
        jButtonAction.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonActionActionPerformed(evt);
            }
        });

        jToolBar1.add(jButtonAction);

        jButtonIssue.setFont(new java.awt.Font("Dialog", 1, 10));
        jButtonIssue.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/velocityme/client/icons/issue-new-32x32.png")));
        jButtonIssue.setToolTipText("Create new issue");
        jButtonIssue.setEnabled(false);
        jButtonIssue.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonIssueActionPerformed(evt);
            }
        });

        jToolBar1.add(jButtonIssue);

        jButtonOrganisation.setFont(new java.awt.Font("Dialog", 1, 10));
        jButtonOrganisation.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/velocityme/client/icons/organisation-new-32x32.png")));
        jButtonOrganisation.setToolTipText("Create new organisation");
        jButtonOrganisation.setEnabled(false);
        jButtonOrganisation.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonOrganisationActionPerformed(evt);
            }
        });

        jToolBar1.add(jButtonOrganisation);

        jButtonDepartment.setFont(new java.awt.Font("Dialog", 1, 10));
        jButtonDepartment.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/velocityme/client/icons/department-new-32x32.png")));
        jButtonDepartment.setToolTipText("Create new department");
        jButtonDepartment.setEnabled(false);
        jButtonDepartment.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonDepartmentActionPerformed(evt);
            }
        });

        jToolBar1.add(jButtonDepartment);

        jButtonPerson.setFont(new java.awt.Font("Dialog", 1, 10));
        jButtonPerson.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/velocityme/client/icons/person-new-32x32.png")));
        jButtonPerson.setToolTipText("Create new person");
        jButtonPerson.setEnabled(false);
        jButtonPerson.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonPersonActionPerformed(evt);
            }
        });

        jToolBar1.add(jButtonPerson);

        jButtonUser.setFont(new java.awt.Font("Dialog", 1, 10));
        jButtonUser.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/velocityme/client/icons/user-new-32x32.png")));
        jButtonUser.setToolTipText("Create new user");
        jButtonUser.setEnabled(false);
        jButtonUser.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonUserActionPerformed(evt);
            }
        });

        jToolBar1.add(jButtonUser);

        jButtonGroup.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/velocityme/client/icons/group-new-32x32.png")));
        jButtonGroup.setToolTipText("Create new group");
        jButtonGroup.setEnabled(false);
        jButtonGroup.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonGroupActionPerformed(evt);
            }
        });

        jToolBar1.add(jButtonGroup);

        jButtonTaskType.setFont(new java.awt.Font("Dialog", 1, 10));
        jButtonTaskType.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/velocityme/client/icons/task-new-32x32.png")));
        jButtonTaskType.setToolTipText("Create new task type");
        jButtonTaskType.setEnabled(false);
        jButtonTaskType.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonTaskTypeActionPerformed(evt);
            }
        });

        jToolBar1.add(jButtonTaskType);

        jButtonState.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/velocityme/client/icons/state-new-32x32.png")));
        jButtonState.setToolTipText("Create new state");
        jButtonState.setEnabled(false);
        jButtonState.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonStateActionPerformed(evt);
            }
        });

        jToolBar1.add(jButtonState);

        jButtonStatus.setFont(new java.awt.Font("Dialog", 1, 10));
        jButtonStatus.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/velocityme/client/icons/status-new-32x32.png")));
        jButtonStatus.setToolTipText("Create new status");
        jButtonStatus.setEnabled(false);
        jButtonStatus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonStatusActionPerformed(evt);
            }
        });

        jToolBar1.add(jButtonStatus);

        jButtonRole.setFont(new java.awt.Font("Dialog", 1, 10));
        jButtonRole.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/velocityme/client/icons/role-new-32x32.png")));
        jButtonRole.setToolTipText("Create new role");
        jButtonRole.setEnabled(false);
        jButtonRole.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRoleActionPerformed(evt);
            }
        });

        jToolBar1.add(jButtonRole);

        jButtonUserAccess.setFont(new java.awt.Font("Dialog", 1, 10));
        jButtonUserAccess.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/velocityme/client/icons/access-new-32x32.png")));
        jButtonUserAccess.setToolTipText("Create new user access");
        jButtonUserAccess.setEnabled(false);
        jButtonUserAccess.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonUserAccessActionPerformed(evt);
            }
        });

        jToolBar1.add(jButtonUserAccess);

        jButtonGroupAccess.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/velocityme/client/icons/groupaccess-new-32x32.png")));
        jButtonGroupAccess.setEnabled(false);
        jButtonGroupAccess.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonGroupAccessActionPerformed(evt);
            }
        });

        jToolBar1.add(jButtonGroupAccess);

        jButtonNodeUserAccess.setFont(new java.awt.Font("Dialog", 1, 10));
        jButtonNodeUserAccess.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/velocityme/client/icons/access-32x32.png")));
        jButtonNodeUserAccess.setToolTipText("View user access");
        jButtonNodeUserAccess.setEnabled(false);
        jButtonNodeUserAccess.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonNodeUserAccessActionPerformed(evt);
            }
        });

        jToolBar1.add(jButtonNodeUserAccess);

        jButtonNodeGroupAccess.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/velocityme/client/icons/groupaccess-32x32.png")));
        jButtonNodeGroupAccess.setEnabled(false);
        jButtonNodeGroupAccess.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonNodeGroupAccessActionPerformed(evt);
            }
        });

        jToolBar1.add(jButtonNodeGroupAccess);

        getContentPane().add(jToolBar1, java.awt.BorderLayout.NORTH);

        jPanel2.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));

        jLabelPerson.setText("Person:");
        jPanel2.add(jLabelPerson);

        jPanel2.add(jLabelPersonValue);

        jLabelUser.setText("User:");
        jPanel2.add(jLabelUser);

        jPanel2.add(jLabelUserValue);

        getContentPane().add(jPanel2, java.awt.BorderLayout.SOUTH);

        jMenuFile.setText("File");
        jMenuItemLogout.setText("Logout");
        jMenuItemLogout.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemLogoutActionPerformed(evt);
            }
        });

        jMenuFile.add(jMenuItemLogout);

        jMenuItemExit.setText("Exit");
        jMenuItemExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemExitActionPerformed(evt);
            }
        });

        jMenuFile.add(jMenuItemExit);

        jMenuBar1.add(jMenuFile);

        jMenuSearch.setText("Search");
        jMenuItemMyResponsibleTasks.setText("My Responsible Tasks");
        jMenuItemMyResponsibleTasks.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemMyResponsibleTasksActionPerformed(evt);
            }
        });

        jMenuSearch.add(jMenuItemMyResponsibleTasks);

        jMenuItemMyInterestedTasks.setText("My Interested Tasks");
        jMenuItemMyInterestedTasks.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemMyInterestedTasksActionPerformed(evt);
            }
        });

        jMenuSearch.add(jMenuItemMyInterestedTasks);

        jMenuItemNewTasks.setText("New Tasks");
        jMenuItemNewTasks.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemNewTasksActionPerformed(evt);
            }
        });

        jMenuSearch.add(jMenuItemNewTasks);

        jMenuItemActiveTasks.setText("Active Tasks");
        jMenuItemActiveTasks.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemActiveTasksActionPerformed(evt);
            }
        });

        jMenuSearch.add(jMenuItemActiveTasks);

        jMenuItemCompletedTasks.setText("Completed Tasks");
        jMenuItemCompletedTasks.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemCompletedTasksActionPerformed(evt);
            }
        });

        jMenuSearch.add(jMenuItemCompletedTasks);

        jMenuItemParameters.setText("Parameters");
        jMenuItemParameters.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemParametersActionPerformed(evt);
            }
        });

        jMenuSearch.add(jMenuItemParameters);

        jMenuItemTime.setText("Time Report");
        jMenuItemTime.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemTimeActionPerformed(evt);
            }
        });

        jMenuSearch.add(jMenuItemTime);

        jMenuBar1.add(jMenuSearch);

        jMenuHelp.setText("Help");
        jMenuItemAbout.setText("About");
        jMenuItemAbout.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemAboutActionPerformed(evt);
            }
        });

        jMenuHelp.add(jMenuItemAbout);

        jMenuBar1.add(jMenuHelp);

        setJMenuBar(jMenuBar1);

        pack();
    }//GEN-END:initComponents

    private void jMenuItemMyInterestedTasksActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemMyInterestedTasksActionPerformed
        // TODO add your handling code here:
        try {
            Collection results = StatelessRemoteClientSessionUtil.getSession().findMyInterestedTasks(m_key, m_nodeValue.getPrimaryKey());
            addTab("My Interested Tasks", new GuiDisplaySearchResults (m_key, m_nodeValue.getPrimaryKey(), results));
        }
        catch(Exception e) {
            ExceptionWindow.showError(e);
        }        
    }//GEN-LAST:event_jMenuItemMyInterestedTasksActionPerformed

    private void jButtonAnchorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAnchorActionPerformed
        // TODO add your handling code here:
        addTab("Create Anchor", new AnchorControl (m_key, m_nodeValue.getPrimaryKey()));        
    }//GEN-LAST:event_jButtonAnchorActionPerformed

    private void jMenuItemCompletedTasksActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemCompletedTasksActionPerformed
        // TODO add your handling code here:
        addTab("Completed Tasks", new GuiDisplayCompletedTasks (m_key, m_nodeValue.getPrimaryKey()));        
    }//GEN-LAST:event_jMenuItemCompletedTasksActionPerformed

    private void jMenuItemActiveTasksActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemActiveTasksActionPerformed
        // TODO add your handling code here:
        try {
            Collection results = StatelessRemoteClientSessionUtil.getSession().findActiveTasks(m_key, m_nodeValue.getPrimaryKey());
            addTab("Active Tasks", new GuiDisplaySearchResults (m_key, m_nodeValue.getPrimaryKey(), results));
        }
        catch(Exception e) {
            ExceptionWindow.showError(e);
        }        
    }//GEN-LAST:event_jMenuItemActiveTasksActionPerformed

    private void jMenuItemNewTasksActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemNewTasksActionPerformed
        // TODO add your handling code here:
        addTab("New Tasks", new GuiDisplayRecentTasks (m_key, m_nodeValue.getPrimaryKey()));
    }//GEN-LAST:event_jMenuItemNewTasksActionPerformed

    private void jButtonNodeGroupAccessActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonNodeGroupAccessActionPerformed
        // TODO add your handling code here:
        addTab("Group Access", new GuiDisplayGroupAccessByNode (m_key, m_nodeValue.getPrimaryKey()));        
    }//GEN-LAST:event_jButtonNodeGroupAccessActionPerformed

    private void jButtonGroupAccessActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonGroupAccessActionPerformed
        // TODO add your handling code here:
        addTab("Create Group Access", new GuiDisplayCreateGroupAccess (m_key, m_nodeValue.getPrimaryKey()));        
    }//GEN-LAST:event_jButtonGroupAccessActionPerformed

    private void jButtonGroupActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonGroupActionPerformed
        // TODO add your handling code here:
        addTab("Create Group", new GroupControl (m_key, m_nodeValue.getPrimaryKey()));        
    }//GEN-LAST:event_jButtonGroupActionPerformed

    private void jMenuItemAboutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemAboutActionPerformed
        // TODO add your handling code here:
        new HelpAbout(this, true, m_key).show();
    }//GEN-LAST:event_jMenuItemAboutActionPerformed

    private void jMenuItemLogoutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemLogoutActionPerformed
        // TODO add your handling code here:
        logoutGui();
    }//GEN-LAST:event_jMenuItemLogoutActionPerformed

    private void jMenuItemExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemExitActionPerformed
        // TODO add your handling code here:
        try {
            m_key.logout();
        }
        catch(RemoteException e) {
            ExceptionWindow.showError(e);
        }
        System.exit(0);
    }//GEN-LAST:event_jMenuItemExitActionPerformed

    private void jTreeNodeMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTreeNodeMouseReleased
        // TODO add your handling code here:
        if(evt.getButton()==evt.BUTTON3 && jTreeNode.getLastSelectedPathComponent() != null){
            m_popup.validate();
            DefaultMutableTreeNode node = (DefaultMutableTreeNode)jTreeNode.getLastSelectedPathComponent();
            NodeValue nodeValue = ((GuiTreeNode)node.getUserObject()).getNodeValue();
            // don't allow system nodes to be cut so they can't be moved.
            m_popup.setCutEnabled(!nodeValue.getFinalType().equals("SystemNode"));
            m_popup.setDeleteEnabled(!nodeValue.getFinalType().equals("SystemNode"));
            if(m_cutNode!=null){
                boolean pasteEnabled = true;
                while(!node.equals(m_guiRootNode)){
                    if(node.equals(m_cutNode)){
                        pasteEnabled = false;
                    }
                    node = (DefaultMutableTreeNode)node.getParent();
                }
                m_popup.setPasteEnabled(pasteEnabled);
            }
            m_popup.show(jTreeNode, evt.getX(), evt.getY() + jScrollPane1.getY());
            evt.consume();
        }        
    }//GEN-LAST:event_jTreeNodeMouseReleased

    private void jButtonStateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonStateActionPerformed
        // Add your handling code here:
        addTab("Create State", new StateControl (m_key, m_nodeValue.getPrimaryKey()));
    }//GEN-LAST:event_jButtonStateActionPerformed

    private void jButtonStatusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonStatusActionPerformed
        // Add your handling code here:
        addTab("Create Status", new StatusControl (m_key, m_nodeValue.getPrimaryKey()));        
    }//GEN-LAST:event_jButtonStatusActionPerformed

    private void jMenuItemTimeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemTimeActionPerformed
        // Add your handling code here:
        addTab("Time Report", new TimeReportPanel (m_key, m_nodeValue.getPrimaryKey()));
    }//GEN-LAST:event_jMenuItemTimeActionPerformed

    private void jButtonDepartmentActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDepartmentActionPerformed
        // Add your handling code here:
        addTab("Create Department", new DepartmentControl (m_key, m_nodeValue.getPrimaryKey()));
    }//GEN-LAST:event_jButtonDepartmentActionPerformed

    private void jButtonOrganisationActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonOrganisationActionPerformed
        // Add your handling code hereUIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());:
        addTab("Create Organisation", new OrganisationControl (m_key, m_nodeValue.getPrimaryKey()));
    }//GEN-LAST:event_jButtonOrganisationActionPerformed

    private void jMenuItemParametersActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemParametersActionPerformed
        // Add your handling code here:
        addTab("Search Parameters", new GuiDisplaySearchParameters (m_key, m_nodeValue.getPrimaryKey()));
    }//GEN-LAST:event_jMenuItemParametersActionPerformed

    private void jMenuItemMyResponsibleTasksActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemMyResponsibleTasksActionPerformed
        // Add your handling code here:
        try {
            Collection results = StatelessRemoteClientSessionUtil.getSession().findMyResponsibleTasks(m_key, m_nodeValue.getPrimaryKey());
            addTab("My Responsible Tasks", new GuiDisplaySearchResults (m_key, m_nodeValue.getPrimaryKey(), results));
        }
        catch(Exception e) {
            ExceptionWindow.showError(e);
        }
    }//GEN-LAST:event_jMenuItemMyResponsibleTasksActionPerformed

    private void jButtonNodeUserAccessActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonNodeUserAccessActionPerformed
        // Add your handling code here:
        addTab("User Access", new GuiDisplayUserAccessByNode (m_key, m_nodeValue.getPrimaryKey()));
    }//GEN-LAST:event_jButtonNodeUserAccessActionPerformed

    private void jButtonUserAccessActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonUserAccessActionPerformed
        // Add your handling code here:
        addTab("Create User Access", new GuiDisplayCreateUserAccess (m_key, m_nodeValue.getPrimaryKey()));
    }//GEN-LAST:event_jButtonUserAccessActionPerformed

    private void jButtonRoleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRoleActionPerformed
        // Add your handling code here:
        addTab("Create Role", new RoleControl (m_key, m_nodeValue.getPrimaryKey()));
    }//GEN-LAST:event_jButtonRoleActionPerformed

    private void jButtonTaskTypeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonTaskTypeActionPerformed
        // Add your handling code here:
        addTab("Create Task Type", new TaskStateMachineControl (m_key, m_nodeValue.getPrimaryKey()));
    }//GEN-LAST:event_jButtonTaskTypeActionPerformed

    private void jButtonUserActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonUserActionPerformed
        // Add your handling code here:
        addTab("Create User", new UserControl (m_key, m_nodeValue.getPrimaryKey()));
    }//GEN-LAST:event_jButtonUserActionPerformed

    private void jButtonPersonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonPersonActionPerformed
        // Add your handling code here:
        addTab("Create Person", new PersonControl (m_key, m_nodeValue.getPrimaryKey()));
    }//GEN-LAST:event_jButtonPersonActionPerformed

    private void jButtonIssueActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonIssueActionPerformed
        // Add your handling code here:
        addTab("Create Issue", new IssueControl (m_key, m_nodeValue.getPrimaryKey()));
    }//GEN-LAST:event_jButtonIssueActionPerformed

    private void jButtonActionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonActionActionPerformed
        // Add your handling code here:
        addTab("Create Action", new ActionControl (m_key, m_nodeValue.getPrimaryKey()));
    }//GEN-LAST:event_jButtonActionActionPerformed

    private void jButtonProjectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonProjectActionPerformed
        // Add your handling code here:
        addTab("Create Project", new ProjectControl (m_key, m_nodeValue.getPrimaryKey()));
    }//GEN-LAST:event_jButtonProjectActionPerformed

    private void jButtonNodeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonNodeActionPerformed
        // Add your handling code here:
        addTab("Create Node", new NodeControl (m_key, m_nodeValue.getPrimaryKey()));
    }//GEN-LAST:event_jButtonNodeActionPerformed

    private void jTreeNodeMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTreeNodeMouseClicked
        // Add your handling code here:
        try {
            if (jTreeNode.getLastSelectedPathComponent() != null) {
                DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode)jTreeNode.getLastSelectedPathComponent();
                GuiTreeNode guiTreeNode = (GuiTreeNode)treeNode.getUserObject();
                guiTreeNode.refreshNodeValue();
                m_guiModelTree.nodeChanged(treeNode);
                m_nodeValue = guiTreeNode.getNodeValue();
                jTabbedPane.setComponentAt(0,guiTreeNode.getDisplay());
                jTabbedPane.setSelectedIndex(0);
                setupToolbar(guiTreeNode);
                // make sure we have all the children of the node in the tree
                Collection guiChildren = guiTreeNode.getChildrenNodeValue();
                Iterator it = guiChildren.iterator();
                while(it.hasNext()) {
                    NodeValue nodeValue = (NodeValue)it.next();
                    boolean existingNode = false;
                    for (Enumeration e = treeNode.children(); e.hasMoreElements(); ) {
                        DefaultMutableTreeNode treeChild = (DefaultMutableTreeNode)e.nextElement();
                        GuiTreeNode guiChildNode = (GuiTreeNode)treeChild.getUserObject();
                        NodeValue childNodeValue = guiChildNode.getNodeValue();
                        if (guiChildNode.getNodeValue().getPrimaryKey().equals(nodeValue.getPrimaryKey())) {
                            existingNode = true;
                            // if the name has changed then update the tree
                            if (!guiChildNode.getNodeValue().getName().equals(nodeValue.getName())) {
                                guiChildNode.setNodeValue(nodeValue);
                                m_guiModelTree.nodeChanged(treeChild);
                            }
                            break;
                        }
                    }
                    if (!existingNode) {
                        DefaultMutableTreeNode treeChild = new DefaultMutableTreeNode(new GuiTreeNode(m_key, nodeValue));
                        treeNode.add(treeChild);
                        m_guiModelTree.nodeStructureChanged(treeNode);
                        jTreeNode.expandPath(new TreePath(treeNode.getPath()));
                    }
                }
            }
        }
        catch(Exception error) {
            ExceptionWindow.showError(error);
        }
    }//GEN-LAST:event_jTreeNodeMouseClicked
    
    /** Exit the Application */
    private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
        logoutGui();
    }//GEN-LAST:event_exitForm
    
    private void setupToolbar(GuiTreeNode p_node) {
        Set setPermissions = p_node.getPermissions();
        jButtonNode.setEnabled(setPermissions.contains(new PermissionPK(new Integer(PermissionBean.NODE_CREATE))));
        jButtonProject.setEnabled(setPermissions.contains(new PermissionPK(new Integer(PermissionBean.PROJECT_CREATE))));
        jButtonAnchor.setEnabled(setPermissions.contains(new PermissionPK(new Integer(PermissionBean.ANCHOR_CREATE))));
        jButtonAction.setEnabled(setPermissions.contains(new PermissionPK(new Integer(PermissionBean.ACTION_CREATE))));
        jButtonIssue.setEnabled(setPermissions.contains(new PermissionPK(new Integer(PermissionBean.ISSUE_CREATE))));
        jButtonOrganisation.setEnabled(m_nodeValue.getName().equals("Setup Organisations") && setPermissions.contains(new PermissionPK(new Integer(PermissionBean.ORGANISATION_CREATE))));
        jButtonDepartment.setEnabled((m_nodeValue.getFinalType().equals("Organisation") || m_nodeValue.getFinalType().equals("Department")) && setPermissions.contains(new PermissionPK(new Integer(PermissionBean.DEPARTMENT_CREATE))));
        jButtonPerson.setEnabled((m_nodeValue.getFinalType().equals("Organisation") || m_nodeValue.getFinalType().equals("Department")) && setPermissions.contains(new PermissionPK(new Integer(PermissionBean.PERSON_CREATE))));
        jButtonUser.setEnabled(m_nodeValue.getName().equals("Setup Users") && setPermissions.contains(new PermissionPK(new Integer(PermissionBean.USER_CREATE))));
        jButtonGroup.setEnabled(m_nodeValue.getName().equals("Setup Groups") && setPermissions.contains(new PermissionPK(new Integer(PermissionBean.GROUP_CREATE))));
        jButtonTaskType.setEnabled(m_nodeValue.getName().equals("Setup Task Types") && setPermissions.contains(new PermissionPK(new Integer(PermissionBean.TASK_TYPE_CREATE))));
        jButtonState.setEnabled(m_nodeValue.getName().equals("Setup States") && setPermissions.contains(new PermissionPK(new Integer(PermissionBean.STATE_CREATE))));
        jButtonStatus.setEnabled(m_nodeValue.getName().equals("Setup Statuses") && setPermissions.contains(new PermissionPK(new Integer(PermissionBean.STATUS_CREATE))));
        jButtonRole.setEnabled(m_nodeValue.getName().equals("Setup Roles") && setPermissions.contains(new PermissionPK(new Integer(PermissionBean.ROLE_CREATE))));
        jButtonUserAccess.setEnabled(setPermissions.contains(new PermissionPK(new Integer(PermissionBean.USER_ACCESS_CREATE))));
        jButtonGroupAccess.setEnabled(setPermissions.contains(new PermissionPK(new Integer(PermissionBean.GROUP_ACCESS_CREATE))));
        jButtonNodeUserAccess.setEnabled(setPermissions.contains(new PermissionPK(new Integer(PermissionBean.USER_ACCESS_CREATE))));
        jButtonNodeGroupAccess.setEnabled(setPermissions.contains(new PermissionPK(new Integer(PermissionBean.GROUP_ACCESS_CREATE))));
    }
    
    
    private static StartGui m_gui = null;
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch(Exception e) {
            ExceptionWindow.showError(e);
        }
        startNewGui();
    }
    
    public static void startNewGui() {
        m_gui = new StartGui();
        m_gui.setLocationRelativeTo(null);
        m_gui.show();
    }
    
    public static void logoutGui() {
        try {
            m_gui.m_key.logout();
        }
        catch(RemoteException e) {
            ExceptionWindow.showError(e);
        }
        shutdownGui();
    }
    
    public static void shutdownGui() {
        m_gui.dispose();
        m_gui = null;
        System.gc();
        startNewGui();
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonAction;
    private javax.swing.JButton jButtonAnchor;
    private javax.swing.JButton jButtonDepartment;
    private javax.swing.JButton jButtonGroup;
    private javax.swing.JButton jButtonGroupAccess;
    private javax.swing.JButton jButtonIssue;
    private javax.swing.JButton jButtonNode;
    private javax.swing.JButton jButtonNodeGroupAccess;
    private javax.swing.JButton jButtonNodeUserAccess;
    private javax.swing.JButton jButtonOrganisation;
    private javax.swing.JButton jButtonPerson;
    private javax.swing.JButton jButtonProject;
    private javax.swing.JButton jButtonRole;
    private javax.swing.JButton jButtonState;
    private javax.swing.JButton jButtonStatus;
    private javax.swing.JButton jButtonTaskType;
    private javax.swing.JButton jButtonUser;
    private javax.swing.JButton jButtonUserAccess;
    private javax.swing.JFrame jFrame1;
    private javax.swing.JLabel jLabelPerson;
    private javax.swing.JLabel jLabelPersonValue;
    private javax.swing.JLabel jLabelUser;
    private javax.swing.JLabel jLabelUserValue;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenu jMenuFile;
    private javax.swing.JMenu jMenuHelp;
    private javax.swing.JMenuItem jMenuItemAbout;
    private javax.swing.JMenuItem jMenuItemActiveTasks;
    private javax.swing.JMenuItem jMenuItemCompletedTasks;
    private javax.swing.JMenuItem jMenuItemExit;
    private javax.swing.JMenuItem jMenuItemLogout;
    private javax.swing.JMenuItem jMenuItemMyInterestedTasks;
    private javax.swing.JMenuItem jMenuItemMyResponsibleTasks;
    private javax.swing.JMenuItem jMenuItemNewTasks;
    private javax.swing.JMenuItem jMenuItemParameters;
    private javax.swing.JMenuItem jMenuItemTime;
    private javax.swing.JMenu jMenuSearch;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JTabbedPane jTabbedPane;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JTree jTreeNode;
    // End of variables declaration//GEN-END:variables
    
    /** Root node of the tree */
    private DefaultMutableTreeNode m_guiRootNode;
    
    /** Model class used for the tree */
    private DefaultTreeModel m_guiModelTree;
    
    private void setupPopupListeners(){
        m_popup.addActionListenerCut(new java.awt.event.ActionListener(){
            public void actionPerformed(java.awt.event.ActionEvent evt){
                processCutAction();
            }
        });
        m_popup.addActionListenerPaste(new java.awt.event.ActionListener(){
            public void actionPerformed(java.awt.event.ActionEvent evt){
                processPasteAction();
            }
        });
        m_popup.addActionListenerDelete(new java.awt.event.ActionListener(){
            public void actionPerformed(java.awt.event.ActionEvent evt){
                processDeleteAction();
            }
        });
    }
    private void processCutAction(){
        try{
            if (jTreeNode.getLastSelectedPathComponent() != null) {
                m_cutNode = (DefaultMutableTreeNode)
                                   jTreeNode.getLastSelectedPathComponent();
                NodeValue nodeValue = ((GuiTreeNode)m_cutNode.getUserObject()).getNodeValue();
                m_popup.setPasteEnabled(true);
                m_popup.setPasteToolTip("Object to paste:" + nodeValue.getName() + "(" + nodeValue.getDescription() + ")");
            }
        }
        catch(Exception ex){
            ExceptionWindow.showError(ex);

        }
    }

    private void processPasteAction(){
        try{
            if (m_cutNode!=null && jTreeNode.getLastSelectedPathComponent() != null) {
                DefaultMutableTreeNode dmtNode = (DefaultMutableTreeNode)
                                   jTreeNode.getLastSelectedPathComponent();

                NodeValue nodeValue = ((GuiTreeNode)dmtNode.getUserObject()).getNodeValue();
                NodeValue childNodeValue = ((GuiTreeNode)m_cutNode.getUserObject()).getNodeValue();

                StatelessRemoteClientSessionUtil.getSession().moveChildNode(m_key, nodeValue.getPrimaryKey(), childNodeValue.getPrimaryKey());
                m_guiModelTree.removeNodeFromParent(m_cutNode);
                m_guiModelTree.insertNodeInto(m_cutNode,dmtNode,0);
                m_cutNode = null;
                m_popup.setPasteEnabled(false);
            }
        }
        catch(Exception ex){
            ExceptionWindow.showError(ex);
        }
    }
    
    private void processDeleteAction() {
        try {
            if (jTreeNode.getLastSelectedPathComponent() != null) {
                if (JOptionPane.showConfirmDialog(this, "Are you sure you want to delete the node?", "Delete Node", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
                    DefaultMutableTreeNode deleteNode = (DefaultMutableTreeNode)jTreeNode.getLastSelectedPathComponent();
                    NodeValue nodeValue = ((GuiTreeNode)deleteNode.getUserObject()).getNodeValue();
                    StatelessRemoteClientSessionUtil.getSession().nodeDelete(m_key, nodeValue.getPrimaryKey());
                    m_guiModelTree.removeNodeFromParent(deleteNode);
                }
            }
        }
        catch(PermissionDeniedException e) {
            JOptionPane.showMessageDialog(this, "You don't have permission to delete the node!", "Permission Denied", JOptionPane.WARNING_MESSAGE);
        }
        catch(Exception ex){
            ExceptionWindow.showError(ex);
        }
    }

}
